package com.wellsoft.mobileoa.utils.widget;

import java.util.ArrayList;
import java.util.List;

import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

import com.wellsoft.mobileoa.R;
import com.wellsoft.mobileoa.utils.BaseActivity;
import com.wellsoft.mobileoa.utils.BaseView;
import com.wellsoft.mobileoa.utils.BaseWidget;
import com.wellsoft.mobileoa.utils.SelectFieldDataAdapter;
import com.wellsoft.mobileoa.utils.SelectOpitonDataAdapter;
import com.wellsoft.mobileoa.utils.SystemConfig;
import com.wellsoft.mobileoa.wsmodel.WSOA_PageField;
import com.wellsoft.mobileoa.wsmodel.WSOA_PageFieldOption;
import com.wellsoft.mobileoa.wsmodel.WSOA_PostField;
import com.wmobile.utils.PopListDialog;

public class CheckboxWidget extends BaseWidget {
	public TextView fieldTitleTv;
	private TextView fieldValueTv;
	private LinearLayout selectLl;
	private List<WSOA_PageFieldOption> dataValue;

	public CheckboxWidget(WSOA_PageField field, BaseView baseView) {
		this.field = field;
		this.baseView = baseView;

		dataValue = new ArrayList<WSOA_PageFieldOption>();
	}

	@Override
	public View getView() {
		view = LayoutInflater.from(baseView.getBaseActivity()).inflate(
				R.layout.checkboxwidget, null);
		fieldTitleTv = (TextView) view
				.findViewById(R.id.checkboxwidget_fieldtitletv);
		fieldValueTv = (TextView) view
				.findViewById(R.id.checkboxwidget_fieldvaluetv);
		fieldTitleTv.setText(field.getFieldTitle());

		selectLl = (LinearLayout) view
				.findViewById(R.id.checkboxwidget_selectll);
		selectLl.setOnClickListener(onSelectListener);

		if (!SystemConfig.stringIsNullOrEmpty(field.getFieldValue())) {
			if (SystemConfig.stringIsNullOrEmpty(field.getSeparator())
					|| field.getFieldValue().indexOf(field.getSeparator()) == -1) {
				for (WSOA_PageFieldOption option : field.getFieldOptions()) {
					if (option.getOptionValue().equals(field.getFieldValue())) {
						if (dataValue.size() == 0) {
							fieldValueTv.setText(option.getMessage());
						} else {
							fieldValueTv.setText(fieldValueTv.getText() + " "
									+ option.getMessage());
						}
						dataValue.add(option);
						if (!SystemConfig.stringIsNullOrEmpty(option
								.getOptionDisable())
								&& option.getOptionDisable().equals("1")) {
							selectLl.setVisibility(View.GONE);
						}
						break;
					}
				}
			} else {
				String[] initValues = field.getFieldValue().split(
						field.getSeparator());
				for (int i = 0; i < initValues.length; i++) {
					for (WSOA_PageFieldOption option : field.getFieldOptions()) {
						if (option.getOptionValue().equals(initValues[i])) {
							if (dataValue.size() == 0) {
								fieldValueTv.setText(option.getMessage());
							} else {
								fieldValueTv.setText(fieldValueTv.getText()
										+ " " + option.getMessage());
							}
							dataValue.add(option);
							if (!SystemConfig.stringIsNullOrEmpty(option
									.getOptionDisable())
									&& option.getOptionDisable().equals("1")) {
								selectLl.setVisibility(View.GONE);
							}
							break;
						}
					}
				}
			}
		} else {
			fieldValueTv.setText("");
		}
		fieldValueTv.setFocusable(false);

		return view;
	}

	@Override
	public WSOA_PostField getValue() {
		WSOA_PostField postfield = new WSOA_PostField();
		postfield.setFieldName(field.getFieldName());
		List<String> listValue = new ArrayList<String>();
		if (dataValue.size() > 0) {
			for (int i = 0; i < dataValue.size(); i++) {
				listValue.add(dataValue.get(i).getOptionValue());
			}
		}
		postfield.setFieldValues(listValue);

		return postfield;
	}

	@Override
	public void setValue(Object widgetValue) {
		List<Integer> values = (ArrayList<Integer>) widgetValue;
		fieldValueTv.setText("");
		dataValue.clear();
		String result = "";
		if (values != null && values.size() > 0) {
			for (int i = 0; i < values.size(); i++) {
				result += field.getFieldOptions().get(values.get(i))
						.getMessage()
						+ " ";
				dataValue.add(field.getFieldOptions().get(values.get(i)));
			}
		}
		fieldValueTv.setText(result);
		baseView.setHasEdited(true);
		setHasEdited(true);
		setHasValided(false);
	}

	@Override
	public void resetWidget() {
		fieldValueTv.setText("");
		dataValue.clear();
	}

	public OnClickListener onSelectListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			List<Integer> selectedIndex = new ArrayList<Integer>();
			for (int i = 0; i < field.getFieldOptions().size(); i++) {
				if (dataValue.contains(field.getFieldOptions().get(i))) {
					selectedIndex.add(Integer.valueOf(i));
				}
			}
			SelectFieldDataAdapter dataAdapter = new SelectFieldDataAdapter(
					baseView.getBaseActivity(), field.getFieldOptions(),
					selectedIndex, true);
			PopListDialog<WSOA_PageFieldOption> dialog = new PopListDialog<WSOA_PageFieldOption>(
					baseView.getBaseActivity(), field.getFieldTitle(),
					selectedIndex, dataAdapter, true);
			if (dialog.showDialog() == PopListDialog.DialogResultOK) {
				setValue(dialog.getSelectedIndex());
			}
		}
	};
}
